export function query2search(data: Record<string, any>) {
  const arr = [];
  for (const d in data) {
    const name = encodeURIComponent(d);
    const value = encodeURIComponent(data[d]);

    arr.push(name + '=' + value);
  }
  return arr.join('&');
}

export function generateArray<T>(len: number, func: (i: number) => T) {
  return Array.from(new Array(len)).map((_, i) => func(i));
}

export function uuid(len = 16) {
  let str = '';
  while (str.length <= len) {
    str += Math.floor(Math.random() * 100000000).toString(16);
  }
  return str.substring(0, len);
}
export function filterTree<T>(
  tree: T[],
  searchValue: any,
  { title = 'title', nodeRender }: { title?: string, nodeRender?: (item: any) => React.ReactNode } = {}
): T[] {
  return tree
    .map((node: any) => {
      const newNode = { ...node }
      newNode.children = node.children ? filterTree(node.children, searchValue, { title }) : []
      if (newNode[title].includes(searchValue) || newNode.children.length) {
        if (nodeRender) {
          newNode[title] = nodeRender(newNode)
        }
        return newNode
      }
      return null
    })
    .filter((node: any) => node !== null)
}